package com.riko.comic.module.comic.controller;


import com.riko.comic.module.comic.bean.po.ChapterComments;
import com.riko.comic.module.comic.bean.vo.ChapterCommentsVo;
import com.riko.comic.module.comic.mapper.ChapterCommentsMapper;
import com.riko.comic.module.comic.service.IChapterCommentsService;
import com.riko.comic.module.sys.bean.po.User;
import com.riko.comic.module.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/chapterComments")
public class ChapterCommentsController {

    @Autowired
    private IChapterCommentsService chapterCommentsService;

    @Autowired
    private ChapterCommentsMapper chapterCommentsMapper;

    @Autowired
    private IUserService userService;

    @RequestMapping("/comments/{chapterId}")
    public ModelAndView Comments(@PathVariable() Integer chapterId , HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();

        List<ChapterCommentsVo> comments = new ArrayList<>();

        for (ChapterComments chapterComments : chapterCommentsMapper.queryComments(chapterId)){
            ChapterCommentsVo chapterCommentsVo = new ChapterCommentsVo();
            chapterCommentsVo.setComments(chapterComments);
            int i = chapterComments.getUserId();
            User user = userService.getById(i);
            chapterCommentsVo.setNick(user.getNick());
            chapterCommentsVo.setPic(user.getPic());

            comments.add(chapterCommentsVo);
        }

        modelAndView.addObject("identity",request.getSession().getAttribute("loginUserId"));
        modelAndView.addObject("comments",comments);
        modelAndView.addObject("id",chapterId);

        modelAndView.setViewName("chapterComments");

        return modelAndView;
    }

    @RequestMapping("/comments/{chapterId}/comment")
    public String comment(@RequestParam(value = "newComment") String newComment, @PathVariable Integer chapterId,HttpServletRequest request){
        HttpSession session = request.getSession();

        ChapterComments comments = new ChapterComments();

        comments.setUserId((Integer) session.getAttribute("loginUserId"));
        comments.setChapterId(chapterId);
        comments.setComment(newComment);
        comments.setCommentTime(LocalDateTime.now());

        chapterCommentsService.saveOrUpdate(comments);


        return "redirect:/chapterComments/comments/"+chapterId;
    }

    @RequestMapping("/comments/{commentId}/delcomment")
    @ResponseBody
    public void delcomment(@PathVariable Integer commentId) {
        chapterCommentsService.removeById(commentId);
    }

}
